import React from "react";
import {Modal, message, Typography} from "antd";
import ExclamationCircleOutlined from '@ant-design/icons/ExclamationCircleOutlined'
import isEmpty from "lodash-es/isEmpty";
import isFunction from "lodash-es/isFunction";
import map from "lodash-es/map";
import globalHelper from "./globalHelper";

const {Paragraph, Text} = Typography;

export const jhConfirm = (props) => {
    const helpers = globalHelper.values || {};
    const {localeUtils} = helpers;
    const {formatMsgByCn} = (localeUtils ? localeUtils : {});
    return Modal.confirm({
        icon: <ExclamationCircleOutlined/>,
        title: formatMsgByCn ? formatMsgByCn('确认操作吗?') : '确认操作吗?',
        centered: true,
...props
})
};


export function jhInfo(props) {
    return Modal.info({
        centered: true,
        ...props
    });
}

export function jhSuccess(props) {
    const helpers = globalHelper.values || {};
    const {localeUtils} = helpers;
    const {formatMsgByCn} = (localeUtils ? localeUtils : {});
    return Modal.success({
        title: formatMsgByCn ? formatMsgByCn('操作成功') : '操作成功',
        centered: true,
        ...props
    });
}

export function jhError(props) {
    const helpers = globalHelper.values || {};
    const {localeUtils} = helpers;
    const {formatMsgByCn} = (localeUtils ? localeUtils : {});
    return Modal.error({
        title: formatMsgByCn ? formatMsgByCn('操作失败') : '操作失败',
        centered: true,
        ...props
    });
}

export function jhWarning(props) {
    const helpers = globalHelper.values || {};
    const {localeUtils} = helpers;
    const {formatMsgByCn} = (localeUtils ? localeUtils : {});
    return Modal.warning({
        centered: true,
        title: formatMsgByCn ? formatMsgByCn('操作提示') : '操作提示',
        ...props
    });
}

export const jhMessage = message;

export const jhErrors = function (props = {}) {
    let {errors, renderValue, modalProps = {}} = props;
    const helpers = globalHelper.values || {};
    const {localeUtils} = helpers;
    const {formatMsgByCn} = (localeUtils ? localeUtils : {});

    if (!isEmpty(errors)) {

        if (!isFunction(renderValue)) {
            renderValue = (val) => (val);
        }

        return Modal.error({
                width: '600px',
                title: <>
            {formatMsgByCn ? formatMsgByCn('温馨提示') : '温馨提示'}
            <Text
        style={{fontWeight: 'normal', float: 'right'}}
        className={'text-primary f12'}
        copyable={{
            text: map(errors, (err, k) => (`${renderValue(err, k)}
`))
        }}>
        {formatMsgByCn ? formatMsgByCn('复制') : '复制'}
    </Text>
        </>,
        content: (
        <Paragraph>
        <ul>
        {
            map(errors, (v, k) => (
        <li key={k} className={'mb8'}>{renderValue(v, k)}</li>
    ))
    }
    </ul>
        </Paragraph>
    ),
        centered: true,
    ...modalProps,
    });
    }
};
export const jhConfirms = function (props = {}) {
    let {warnings, renderValue, modalProps = {}, title} = props;
    const helpers = globalHelper.values || {};
    const {localeUtils} = helpers;
    const {formatMsgByCn} = (localeUtils ? localeUtils : {});

    if (!isEmpty(warnings)) {

        if (!isFunction(renderValue)) {
            renderValue = (val) => (val);
        }

        return Modal.confirm({
                width: '600px',
                centered: true,
                title: <>
            {title ? title : (formatMsgByCn ? formatMsgByCn('温馨提示') : '温馨提示')}
            <Text
        style={{fontWeight: 'normal', float: 'right'}}
        className={'text-primary f12'}
        copyable={{
            text: map(warnings, (err, k) => (`${renderValue(err, k)}
`))
        }}>
        {formatMsgByCn ? formatMsgByCn('复制') : '复制'}
    </Text>
        </>,
        content: (
        <Paragraph>
        <ul>
        {
            map(warnings, (v, k) => (
        <li key={k} className={'mb8'}>{renderValue(v, k)}</li>
    ))
    }
    </ul>
        </Paragraph>
    ),
    ...modalProps,
    });
    }
}